import { storage, tokenTableName } from '@/config'

/**
 * @author chuzhixin 1204505056@qq.com （不想保留author可删除）
 * @description 获取accessToken
 * @returns {string|ActiveX.IXMLDOMNode|Promise<any>|any|IDBRequest<any>|MediaKeyStatus|FormDataEntryValue|Function|Promise<Credential | null>}
 */
export function getAccessToken () {
  if (storage) {
    if ('localStorage' === storage) {
      return localStorage.getItem(tokenTableName)
    } else if ('sessionStorage' === storage) {
      return sessionStorage.getItem(tokenTableName)
    } else {
      return localStorage.getItem(tokenTableName)
    }
  } else {
    return localStorage.getItem(tokenTableName)
  }
}

/**
 * @author chuzhixin 1204505056@qq.com （不想保留author可删除）
 * @description 存储accessToken
 * @param accessToken
 * @returns {void|*}
 */
export function setAccessToken (accessToken) {
  if (storage) {
    if ('localStorage' === storage) {
      return localStorage.setItem(tokenTableName, accessToken)
    } else if ('sessionStorage' === storage) {
      return sessionStorage.setItem(tokenTableName, accessToken)
    } else {
      return localStorage.setItem(tokenTableName, accessToken)
    }
  } else {
    return localStorage.setItem(tokenTableName, accessToken)
  }
}

export function getEmpInfo () {
  var res = ''
  if (storage) {
    if ('localStorage' === storage) {
      res = localStorage.getItem('currentEmp')
    } else if ('sessionStorage' === storage) {
      res = sessionStorage.getItem('currentEmp')
    } else {
      res = localStorage.getItem('currentEmp')
    }
  } else {
    res = localStorage.getItem('currentEmp')
  }
  if (res) {
    try {
      return JSON.parse(res)
    }
    catch {
      return null
    }
  }
  return null
}

export function setEmpInfo (employee) {
  let emp = JSON.stringify(employee)
  if (storage) {
    if ('localStorage' === storage) {
      return localStorage.setItem('currentEmp', emp)
    } else if ('sessionStorage' === storage) {
      return sessionStorage.setItem('currentEmp', emp)
    } else {
      return localStorage.setItem('currentEmp', emp)
    }
  } else {
    return localStorage.setItem('currentEmp', emp)
  }
}
/**
 * @author chuzhixin 1204505056@qq.com （不想保留author可删除）
 * @description 移除accessToken
 * @returns {void|Promise<void>}
 */
export function removeAccessToken () {
  if (storage) {
    if ('localStorage' === storage) {
      return localStorage.removeItem(tokenTableName)
    } else if ('sessionStorage' === storage) {
      return sessionStorage.clear()
    } else {
      return localStorage.removeItem(tokenTableName)
    }
  } else {
    return localStorage.removeItem(tokenTableName)
  }
}
